package com.tour.silvertour.Controller;

import com.alibaba.fastjson2.JSON;
import com.tour.silvertour.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Title: BaseController
 * @Author 苗桐菲
 * @Package com.tour.silvertour.Controller
 * @Date 2024/3/31 14:16
 */
// 基础controller，通用的方法放里面，供其他controller继承
public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Resource
    protected RedisTemplate redisTemplate;

    // 从request中获取用户id
    public Long getId() {
        return (Long) request.getAttribute("id");
    }


    // 获取当前登录用户信息
    public User getCurUser() {
        String token = request.getHeader("token");
        String userJson = (String) redisTemplate.opsForValue().get(token);
        User curUser = JSON.parseObject(userJson,User.class);
        return curUser;
    }
}
